package gui;

import java.awt.BorderLayout;
import javax.swing.*;

import java.awt.event.*;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import engine.ReadXMLFile;
import engine.SvgObjects;

// test commit
import java.io.*;

@SuppressWarnings("serial")
public class MainFrame extends JFrame
{
    
    private static DrawShapes content;
    private JFileChooser      fileopen;
    
    /**
     * This is the default constructor
     */
    public MainFrame()
    {
        super();
        initialize();
    }
    
    /**
     * This is the initialise method, this method contain add menubar, menus and
     * menu items
     */
    private void initialize()
    {
        this.setSize(800, 600);
        this.setNativeLookAndFeel();
        setLocationRelativeTo(this);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        fileopen = new JFileChooser();
        // Add Menu Bar
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        // Menu Items
        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);
        JMenuItem mntmOpen = new JMenuItem("Open");
        mntmOpen.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                ListenerPerformed(arg0);
            }
        });
        mnFile.add(mntmOpen);
        
        JMenuItem mntmQuit = new JMenuItem("Quit");
        mntmQuit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                ListenerPerformed(arg0);
            }
        });
        mnFile.add(mntmQuit);
        JMenu mnHelp = new JMenu("Help");
        menuBar.add(mnHelp);
        JMenuItem mntmAbout = new JMenuItem("About");
        mnHelp.add(mntmAbout);
        setContentPane(getJContentPanel());
        
    }
    
    /**
     * This getJContenPanel method is the initialize method for DrawShapes which
     * implement from JPanel
     * 
     * @return DrawShapes
     */
    public DrawShapes getJContentPanel()
    {
        if (content == null)
        {
            content = new DrawShapes();
            content.setLayout(new BorderLayout());
        }
        return content;
    }
    
    /**
     * This method make JFrame to OS like
     */
    private void setNativeLookAndFeel()
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e)
        {
        }
    }
    
    /**
     * This method is the Listener Action, which is for Open menu Listener and
     * for Quit menu listener
     * 
     * @param arg0
     */
    private void ListenerPerformed(ActionEvent arg0)
    {
        if (arg0.getActionCommand().equals("Open"))
        {
            FileFilter filter = new FileNameExtensionFilter("svg files", "svg");
            fileopen.addChoosableFileFilter(filter);
            int ret = fileopen.showDialog(this, "Open file");
            if (ret == JFileChooser.APPROVE_OPTION)
            {
                File file = fileopen.getSelectedFile();
                ReadXMLFile rd = new ReadXMLFile();
                rd.readIn(file.toString());
                SvgObjects ob = rd.getObjectsss();
                content.addOB(ob);
                SwingUtilities.updateComponentTreeUI(this);
            }
        }
        else if (arg0.getActionCommand().equals("Quit"))
        {
            dispose();
        }
    }
    
    /**
     * Add Shape or Object into the DrawShape Panel
     * 
     * @param ob
     *            SvgObjects
     */
    public void addSvgOb(SvgObjects ob)
    {
        content.addOB(ob);
        content.reload();
    }
    
    /**
     * To refresh Jpanel
     */
    public void refresh()
    {
        content.reload();
    }
}
